package com.ruoyi.supplier.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.excel.utils.ExcelUtil;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.supplier.domain.Categories;
import com.ruoyi.supplier.domain.vo.cate.CateVo;
import com.ruoyi.supplier.manager.CategoryManager;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.supplier.domain.vo.CategoriesVo;
import com.ruoyi.supplier.domain.bo.CategoriesBo;
import com.ruoyi.supplier.service.ICategoriesService;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;

import java.util.List;
import java.util.Arrays;
import java.util.Objects;
import java.util.stream.Collectors;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.servlet.http.HttpServletResponse;

/**
 * 商品分类控制器
 * 前端访问路由地址为:/supplier/categories
 *
 * @author ruoyi
 * @date 2023-09-18
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/category")
public class CategoriesController extends BaseController {

    private final ICategoriesService iCategoriesService;

    @Autowired
    private CategoryManager categoryManager;

    /**
     * 查询商品分类列表
     */
    @SaCheckPermission("supplier:categories:list")
    @GetMapping("/list")
    public TableDataInfo<CategoriesVo> list(CategoriesBo bo, PageQuery pageQuery) {
        return iCategoriesService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出商品分类列表
     */
    @SaCheckPermission("supplier:categories:export")
    @Log(title = "商品分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(CategoriesBo bo, HttpServletResponse response) {
        List<CategoriesVo> list = iCategoriesService.queryList(bo);
        ExcelUtil.exportExcel(list, "商品分类", CategoriesVo.class, response);
    }

    /**
     * 获取商品分类详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("supplier:categories:query")
    @GetMapping("/{id}")
    public R<CategoriesVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return R.ok(iCategoriesService.queryById(id));
    }

    /**
     * 新增商品分类
     */
    @SaCheckPermission("supplier:categories:add")
    @Log(title = "商品分类", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody CategoriesBo bo) {
        return toAjax(iCategoriesService.insertByBo(bo));
    }

    /**
     * 修改商品分类
     */
    @SaCheckPermission("supplier:categories:edit")
    @Log(title = "商品分类", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody CategoriesBo bo) {
        return toAjax(iCategoriesService.updateByBo(bo));
    }

    /**
     * 删除商品分类
     *
     * @param ids 主键串
     */
    @SaCheckPermission("supplier:categories:remove")
    @Log(title = "商品分类", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
        return toAjax(iCategoriesService.deleteWithValidByIds(Arrays.asList(ids), true));
    }


    /**
     * 查询商品分类
     * @return
     */
    @GetMapping("/index")
    public R<List<CategoriesVo>> index() {

        List<CateVo> cateVos = categoryManager.getAllTree();
        return R.ok();
    }


}
